package com.kzx.ticket.utils;

import com.kzx.ticket.model.MovieMsg;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author kongz
 * @version 1.0 2021/11/16 20:08
 * @description excel工具类
 */
public class ExcelUtils {
    public static void write(String fileName, String path, List<MovieMsg> list) {
        HSSFWorkbook w = new HSSFWorkbook();
        HSSFSheet sheet = w.createSheet("票房信息");
        HSSFRow head = sheet.createRow(0);

        //第一行
        head.createCell(0).setCellValue("排名");
        head.createCell(1).setCellValue("电影编号");
        head.createCell(2).setCellValue("??电影编号");
        head.createCell(3).setCellValue("电影名");
        head.createCell(4).setCellValue("电影英文名");
        head.createCell(5).setCellValue("年度票房");
        head.createCell(6).setCellValue("年度场次");
        head.createCell(7).setCellValue("总人次");
        head.createCell(8).setCellValue("平均票价");
        head.createCell(9).setCellValue("场均人次");
        head.createCell(10).setCellValue("上映日期");
        head.createCell(11).setCellValue("总票房");
        head.createCell(12).setCellValue("总场次");
        head.createCell(13).setCellValue("总人次");
        head.createCell(14).setCellValue("首周票房");
        head.createCell(15).setCellValue("上映年份");
        int row = 1;
        for (MovieMsg movieMsg : list) {
            HSSFRow body = sheet.createRow(row++);
            body.createCell(0).setCellValue(movieMsg.getIRank());
            body.createCell(1).setCellValue(movieMsg.getMovieID());
            body.createCell(2).setCellValue(movieMsg.getEntMovieID());
            body.createCell(3).setCellValue(movieMsg.getMovieName());
            body.createCell(4).setCellValue(movieMsg.getEnMovieName());
            body.createCell(5).setCellValue(movieMsg.getBoxOffice());
            body.createCell(6).setCellValue(movieMsg.getShowCount());
            body.createCell(7).setCellValue(movieMsg.getAudienceCount());
            body.createCell(8).setCellValue(movieMsg.getAvgBoxOffice());
            body.createCell(9).setCellValue(movieMsg.getAvgShowAudienceCount());
            body.createCell(10).setCellValue(movieMsg.getReleaseDate());
            body.createCell(11).setCellValue(movieMsg.getTotalBoxOffice());
            body.createCell(12).setCellValue(movieMsg.getTotalShowCount());
            body.createCell(13).setCellValue(movieMsg.getTotalAudienceCount());
            body.createCell(14).setCellValue(movieMsg.getWeekBoxOffice());
            body.createCell(15).setCellValue(movieMsg.getYear());
        }
        try {
            File parent = new File(path);
            if (!parent.exists()) {
                parent.mkdir();
            }

            FileOutputStream fileOutputStream = new FileOutputStream(path + fileName);
            w.write(fileOutputStream);
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
